from typing import Optional, List, Generic, Union, Any, TypeVar

from pydantic import BaseModel, Field

from app.api.database.models.llm import LLMServerBase

# 创建泛型变量
DataT = TypeVar('DataT')


class UnifiedResponseModel(BaseModel, Generic[DataT]):
    """统一响应模型"""
    status_code: int
    status_message: str
    data: DataT = None


def resp_200(data: Union[list, dict, str, Any] = None,
             message: str = 'SUCCESS') -> UnifiedResponseModel:
    """成功的代码"""
    return UnifiedResponseModel(status_code=200, status_message=message, data=data)
    # return ret.model_dump()


def resp_500(code: int = 500,
             data: Union[list, dict, str, Any] = None,
             message: str = 'BAD REQUEST') -> UnifiedResponseModel:
    """错误的逻辑回复"""
    return UnifiedResponseModel(status_code=code, status_message=message, data=data)


class LLMModelCreateReq(BaseModel):
    id: Optional[int] = Field(default=None, description='模型唯一ID, 更新时需要传')
    server_id: int = Field(..., description='服务提供方ID')
    name: str = Field(..., description='模型展示名称')
    description: Optional[str] = Field(default='', description='模型描述')
    model_name: str = Field(..., description='模型名称')
    model_type: str = Field(..., description='模型类型')
    model_category: list[str] = Field(..., description='模型分类')
    model_keys: list[str] = Field(..., description='模型密钥列表')
    online: bool = Field(default=True, description='是否在线')
    remark: Optional[str] = Field(default='', description='备注')
    config: Optional[dict] = Field(default=None, description='模型配置')


class LLMServerCreateReq(BaseModel):
    id: Optional[int] = Field(default=None, description='服务提供方ID, 更新时需要传')
    name: str = Field(..., description='服务提供方名称')
    description: Optional[str] = Field(default='', description='服务提供方描述')
    server_type: str = Field(..., description='服务提供方类型')
    # server_api_type: str = Field(..., description='服务提供方接口类型')
    config: Optional[dict] = Field(default=None, description='服务提供方配置')


# 模型调用

class LLMInvokeReq(BaseModel):
    model_id: int = Field(..., description='模型ID')
    messages: List[dict] = Field(..., description='消息列表')
    config: Optional[dict] = Field(default=None, description='模型其他配置')

class EmbeddingReq(BaseModel):
    model_id: int = Field(..., description='模型ID')
    texts: list[str] = Field(..., description='文本数组')
    config: Optional[dict] = Field(default=None, description='模型其他配置')
class LLMServerInfo(LLMServerBase):
    id: Optional[int] = None
